package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.dao.BlogListDao;
import cn.pojo.BlogList;

public class BlogListDaoImpl implements BlogListDao {
/**
 * 添加类实现(数据库操作)
 * executeUpdate()方法处理增删改操作执行
 * @throws SQLException 
 */
	public Boolean addBlog(Connection connection,BlogList bloglist) throws SQLException {
		boolean flag = false;
		String sql = "insert into blogs(title,content,author,categroy) value(?,?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, bloglist.getTitle());
		ps.setString(2, bloglist.getContent());
		ps.setString(3, bloglist.getAuthor());
		ps.setString(4, bloglist.getCategroy());
		int count = ps.executeUpdate();
		if(count == 1) {
			flag = true;
		}else {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 删除类实现
	 */
	public Boolean deleteBlogByBlogId(Connection connection,Integer id) throws SQLException {
		boolean flag = false;
		String sql = "delete from blogs where id=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setInt(1, id);
		int count = ps.executeUpdate();
		if(count == 1) {
			flag = true;
		}else {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 修改类实现
	 * @throws SQLException 
	 */
	public Boolean updateBlog(Connection connection,BlogList blog) throws SQLException {
		boolean flag = false;
		String sql = "update blogs set title=?,content=?,categroy=? where id=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, blog.getTitle());
		ps.setString(2, blog.getContent());
		ps.setString(3, blog.getCategroy());
		ps.setInt(3, blog.getId());
		int count = ps.executeUpdate();
		if(count == 1) {
			flag = true;
		}else {
			flag = false;
		}
		return flag;
	}
}
